package dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import dao.AccountDao;
import dbpool.DbPool;
import entity.Account;

public class AccountDaoImpl implements AccountDao{
	DbPool pool = DbPool.getPool();
	/**
	 * 
	 * 
	 * @return	根据用户id找到用户所用有账号的全部大区
	 * @throws SQLException
	 */
	@Override
	public List<String> getAllPart(int userid) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "select count_part from account where user_id=?";
		QueryRunner runner = new QueryRunner();
		List<String> partList = (List<String>) runner.query(conn, sql, new BeanListHandler<String>(String.class),userid);
		pool.back(conn);
		return partList;
	}
	/**
	 * 
	 * @param part 大区
	 * @return	根据所选的大区获得当前大区所有的服务器
	 * @throws SQLException
	 */
	@Override
	public List<String> getSeverByPart(String part,int userid) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "select count_server from account where user_id=? and count_part=?";
		QueryRunner runner = new QueryRunner();
		List<String> serverList = (List<String>) runner.query(conn, sql, new BeanListHandler<String>(String.class),userid,part);
		pool.back(conn);
		return serverList;
	}
	/**
	 * 
	 * @param server 选择的服务器
	 * @param user_id 用户id
	 * @return	根据用户选择的服务器和用户id查找到角色名称
	 * @throws SQLException
	 */
	@Override
	public String getRoleByUser(String server, int user_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "select count_server from account where user_id=? and count_server=?";
		QueryRunner runner = new QueryRunner();
		Account a = (Account) runner.query(conn, sql, new BeanHandler<Account>(Account.class),user_id,server);
		pool.back(conn);
		return a.getCount_name();
	}

}
